package dataStruct.queue;

import jdk.jfr.DataAmount;
import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public class DelayedTask implements Delayed {
    private long expireTime; // 过期时间
    private String taskName; // 任务名称

    public DelayedTask(long expireTime, String taskName) {
        this.expireTime = expireTime;
        this.taskName = taskName;
    }
    // 返回当前元素的剩余时间(即当对象还有多久才会到期)
    @Override
    public long getDelay(TimeUnit unit) { // 指定时间单位
        return unit.convert(expireTime -
                System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        // 最早到期的任务排在队列的最前面
        return Long.compare(this.expireTime, ((DelayedTask)o).expireTime);
    }

    @Override
    public String toString() {
        return "DelayedTask{" +
                "expireTime=" + expireTime +
                ", taskName='" + taskName + '\'' +
                '}';
    }
}
